import axios from 'axios'
import { API_URL, API_TIMEOUT } from '../config/api'

const apiClient = axios.create({
  baseURL: API_URL,
  timeout: API_TIMEOUT,
  headers: {
    'Content-Type': 'application/json'
  }
})

// Add token to requests if available
apiClient.interceptors.request.use(config => {
  const token = localStorage.getItem('auth_token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
})

export function login(username, password) {
  return apiClient.post('/auth/login', { username, password })
}

export function logout() {
  return apiClient.post('/auth/logout')
}

export function changePassword(oldPassword, newPassword) {
  return apiClient.post('/auth/change-password', { oldPassword, newPassword })
}

export function getCurrentUser() {
  return apiClient.get('/auth/user')
}

export function checkAuthStatus() {
  const token = localStorage.getItem('auth_token')
  return !!token
} 